package com.jwsoft.manager.core.integration.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.data.core.util.PagerUtil;
import com.bifang.module.base.core.helper.DictHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.enums.EduAreaEnum;
import com.jwsoft.manager.common.vo.eduArea.EduAreaKeyVO;
import com.jwsoft.manager.common.vo.eduArea.EduAreaQueryVO;
import com.jwsoft.manager.common.vo.eduArea.EduAreaVO;
import com.jwsoft.manager.core.dao.model.EduArea;
import com.jwsoft.manager.core.dao.service.EduAreaService;
import com.jwsoft.manager.core.integration.EduAreaIntegration;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;
/**
 * 行政区划参数维护业务实现类
 *
 * @author dlb
 * @since 2023-02-09
 */
@ApiService(funcCode = "eduAreaLocalManage", title = "行政区划参数维护")
public class EduAreaIntegrationImpl implements EduAreaIntegration {

    @Autowired
    public EduAreaService eduAreaService;

    @Autowired
    private DictHelper dictHelper;

    @Override
    @OpApi(funcCode = "eduAreaLocalManage0001", title = "行政区划参数维护分页查询", funcType = FuncTypeEnum.query)
    public PageInfo<EduAreaVO> getList(EduAreaQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        LambdaQueryWrapper<EduArea> eduAreaQueryWrapper = new LambdaQueryWrapper<>();
        eduAreaQueryWrapper.like(!ObjectUtils.isEmpty(vo.getAreaCode()),EduArea::getAreaCode,vo.getAreaCode())
                            .like(!ObjectUtils.isEmpty(vo.getLocalCode()),EduArea::getLocalCode,vo.getLocalCode())
                            .like(!ObjectUtils.isEmpty(vo.getAreaName()),EduArea::getAreaName,vo.getAreaName())
                            .eq(!ObjectUtils.isEmpty(vo.getOrgType()),EduArea::getOrgType,vo.getOrgType());
        List<EduArea> list = eduAreaService.list(eduAreaQueryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduArea> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduAreaVO> resultList = Convert.toList(EduAreaVO.class, pageInfo.getList());
        // 转义数据字典值
        dictHelper.valueToName(resultList, Arrays.asList(EduAreaEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "eduAreaLocalManage0002", title = "行政区划参数维护根据主键查询详情", funcType = FuncTypeEnum.query)
    public EduAreaVO getById(EduAreaKeyVO vo) {
        EduArea entity = eduAreaService.getById(vo.getAreaId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        EduAreaVO result = Convert.convert(EduAreaVO.class, entity);
        return result;
    }
    /** 保存前统一校验*/
    @Override
    @OpApi(funcCode = "eduAreaLocalManage0003", title = "行政区划参数维护保存前校验", funcType = FuncTypeEnum.other ,checkPrivilege = BoolEnum.FALSE)
    public void checkSave(EduAreaVO vo) {
            if(StringUtils.isEmpty(vo.getAreaCode())){
                throw new AppException("未填写标准编码！");
            }
            if(StringUtils.isEmpty(vo.getLocalCode())){
                throw new AppException("未填写本地编码！");
            }
            if(StringUtils.isEmpty(vo.getAreaName())){
                throw new AppException("未填写组织名称！");
            }
            if(StringUtils.isEmpty(vo.getOrgType())){
                throw new AppException("未选择行政区划类型！");
            }
            if(StringUtils.isEmpty(vo.getStatus())){
                throw new AppException("未选择状态！");
            }
            LambdaQueryWrapper<EduArea> eduAreaQueryWrapper = new LambdaQueryWrapper<>();
                eduAreaQueryWrapper.eq(EduArea::getAreaCode,vo.getAreaCode())
                    .eq(EduArea::getLocalCode,vo.getLocalCode())
                        .notIn(!StringUtils.isBlank(vo.getAreaId()),EduArea::getAreaId,vo.getAreaId());
             EduArea eduArea = eduAreaService.getOne(eduAreaQueryWrapper);
            if(!ObjectUtils.isEmpty(eduArea)){
                throw new AppException("有重复数据，请在原数据进行修改");
            }
    }

    @Override
    @OpApi(funcCode = "eduAreaLocalManage0004", title = "行政区划参数维护新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduAreaLocalManage0004",
            operationName = "行政区划参数维护新增",
            dataType = "areaId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduAreaVO add(EduAreaVO vo) {
        //保存前校验
        checkSave(vo);
        //保存数据
        EduArea entity = Convert.convert(EduArea.class, vo);
        eduAreaService.save(entity);
        //将主键设置回vo使操作日志可以取到
        vo.setAreaId(entity.getAreaId());
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduAreaLocalManage0005", title = "行政区划参数维护修改", funcType = FuncTypeEnum.update)
    //关闭日志
//    @SaveOplog(
//            operationType = "eduAreaLocalManage0005",
//            operationName = "行政区划参数维护修改",
//            dataType = "areaId",
//            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduAreaVO edit(EduAreaVO vo) {
        if (ObjectUtils.isEmpty(vo.getAreaId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        EduArea oldEntity = eduAreaService.getById(vo.getAreaId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        EduArea entity = Convert.convert(EduArea.class, vo);
        eduAreaService.updateById(entity);
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduAreaLocalManage0006", title = "行政区划参数维护根据主键删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "eduAreaLocalManage0006",
            operationName = "行政区划参数维护删除",
            dataType = "areaId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(EduAreaKeyVO vo) {
        if(!StringUtils.isBlank(vo.getAreaId())){
            EduArea eduArea = eduAreaService.getById(vo.getAreaId());
            if (ObjectUtils.isEmpty(eduArea)) {
                throw new AppException("未找到需要删除的信息!");
            }
            eduAreaService.removeById(eduArea);
        }else{
            List<String> ids = vo.getAreaIds();
            List<EduArea> list = eduAreaService.listByIds(ids);
            if (CollectionUtils.isEmpty(list)) {
                throw new AppException("未找到需要删除的信息!");
            }
            eduAreaService.removeByIds(ids);
        }
    }
}
